﻿using B.S.BasicData.Api.Read.Application.Command.GridPerson;
using B.S.BasicData.Api.Read.Dtos;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.CommandHandler.GridPersion
{
    /// <summary>
    /// 获取网格人员中介者处理类
    /// </summary>
    public class GetGridPersonHandler : IRequestHandler<GetGridPersonCommand, ApiPaging<AccountDto>>
    {
        private readonly IBaseRepository<AccountModel> accRepo;//账号的仓储
        private readonly IBaseRepository<RoleModel> roleRepo;//角色仓储
        private readonly IBaseRepository<DepartmentModel> departRepo;//部门仓储

        /// <summary>
        /// 通过构造函数注入中介者需要的各种资源
        /// </summary>
        /// <param name="accRepo">账号仓储</param>
        /// <param name="roleRepo">角色仓储</param>
        /// <param name="departRepo">部门仓储</param>
        public GetGridPersonHandler(IBaseRepository<AccountModel> accRepo,IBaseRepository<RoleModel> roleRepo,IBaseRepository<DepartmentModel> departRepo)
        {
            this.accRepo = accRepo;
            this.roleRepo = roleRepo;
            this.departRepo = departRepo;
        }
        /// <summary>
        /// 获取网格人员分页列表中介者处理方法
        /// </summary>
        /// <param name="request">获取网格人员列表参数命令(协议)</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>用Task包装的分页的网格人员列表</returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPaging<AccountDto>> Handle(GetGridPersonCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<AccountDto> result = new ApiPaging<AccountDto>();//实例化了一个响应包装类
            var accounts = accRepo.GetAll().Where(x=>x.IsDeleted==false&&x.RoleId!=1); //从仓储中拉取所有符合条件的账号数据
            var roles = roleRepo.GetAll().Where(x=>x.IsDeleted==false);//从仓储中拉取所有符合条件的角色数据
            var departs = departRepo.GetAll().Where(x=>x.IsDeleted==false);//从仓储中拉取所有符合条件的部门数据
            if (!string.IsNullOrEmpty(request.DepartName)) {//如果部门名称条件不为空,就按照部门名称查询
                departs = departs.Where(x=>x.DepartName.Contains(request.DepartName));
            }
            if (!string.IsNullOrEmpty(request.RealName))//如果真实姓名不为空,就把真实姓名作为条件进行查询
            {
                accounts = accounts.Where(x => x.RealName.Contains(request.RealName));
            }
            //linq 查询
            var linq = from a in accounts
                       join d in departs on a.DepartmentId equals d.Id
                       join r in roles on a.RoleId equals r.Id
                       select new AccountDto
                       { 
                           Id=a.Id,
                           Name = a.Name,
                           RealName = a.RealName,
                           Phone = a.Phone,
                           Card = a.Card,
                           RoleId = r.Id,
                           RoleName = r.RoleName,
                           DepartmentId = a.DepartmentId,
                           DepartmentName = d.DepartName
                       };

            result.TotalCount = linq.Count();//总条数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);//总页数
            //分页的数据
            result.Items = linq.OrderByDescending(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            result.Msg = "获取网格人员分页列表成功";//响应消息
            result.Code = ApiEnums.Success;//响应状态码
            return Task.FromResult(result);
        }



        ///// <summary>
        ///// 获取网格人员分页列表中介者处理方法
        ///// </summary>
        ///// <param name="request">命令</param>
        ///// <param name="cancellationToken">取消</param>
        ///// <returns>异步的网格人员Dto分页包装</returns>
        //public Task<ApiPaging<AccountDto>> Handle(GetGridPersonCommand request, CancellationToken cancellationToken)
        //{
        //    ApiPaging<AccountDto> result = new ApiPaging<AccountDto>();//分页响应包装类

        //    var accounts = accRepo.GetAll().Where(x=>x.IsDeleted==false&&x.RoleId!=1); //获取所有未被删除的非管理员账号
        //    var roles = roleRepo.GetAll();
        //    var departs = departRepo.GetAll();

        //    //先判断条件,锁定目标
        //    if (!string.IsNullOrEmpty(request.DepartName)) {
        //        departs = departs.Where(x=>x.DepartName.Contains(request.DepartName));
        //    }
        //    if (!string.IsNullOrEmpty(request.RealName))
        //    {
        //        accounts = accounts.Where(x => x.RealName.Contains(request.RealName));
        //    }


        //    //关联账号 角色 部门,取出dto里的字段
        //    var linq = from a in accounts
        //               join r in roles on a.RoleId equals r.Id
        //               join d in departs on a.DepartmentId equals d.Id
        //               select new AccountDto
        //               {
        //                   Id =a.Id,
        //                   Name =a.Name,
        //                   RealName =a.RealName,
        //                   Card = a.Card,
        //                   Phone = a.Phone,
        //                   RoleId =a.RoleId,
        //                   DepartmentId =a.DepartmentId,
        //                   RoleName = r.RoleName,
        //                   DepartmentName = d.DepartName
        //               };
        //    //总条数 总页数
        //    result.TotalCount = linq.Count();
        //    result.TotalPage = (int)Math.Ceiling(result.TotalCount*1.0 / request.PageSize);
        //    result.Items = linq.OrderByDescending(x=>x.Id)
        //                        .Skip((request.PageIndex-1)*request.PageSize)
        //                        .Take(request.PageSize)
        //                        .ToList();

        //    result.Msg = "获取网格人员分页数据成功!";
        //    result.Code = ApiEnums.Success;


        //    return Task.FromResult(result);


        //}
    }
}
